//
//  DataManagerJson.m
//  MessangerSkeleton
//
//  Created by User-23 on 4/29/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "HighScoresDataManagerJson.h"

@interface HighScoresDataManagerJson ()

@property(nonatomic) NSMutableArray* scores;
@property(nonatomic) NSMutableArray* names;

@property(nonatomic) NSMutableData* downloadedData;

@end


@implementation HighScoresDataManagerJson

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.scores = [[NSMutableArray alloc] init];
        self.names = [[NSMutableArray alloc] init];
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://78.90.84.177/index.json"]] delegate:self];
        //[self postJson];
        }
    return self;
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    self.downloadedData = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.downloadedData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self readJson];
}

-(void) readJson {
    NSMutableString* stringData = [[NSMutableString alloc] initWithData:self.downloadedData encoding:NSUTF8StringEncoding];
//    NSLog(@"%@",stringData);
    id parsedData = [NSJSONSerialization JSONObjectWithData:[stringData dataUsingEncoding:NSUTF8StringEncoding]  options:NSJSONReadingMutableLeaves error:nil];
    if ([parsedData isKindOfClass:[NSDictionary class]]) {
        NSDictionary* myData = (NSDictionary*) parsedData;
        NSLog(@"Mydata%@",myData);
        NSLog(@"I want mitko%@",myData[@"?"]);
//        [items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//            NSDictionary* item = (NSDictionary*) obj;
//            [self.scores addObject:item[@"date_taken"]];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"NewTitle" object:self.scores];
//            
//            [self.links addObject:item[@"media"][@"m"]];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"NewLink" object:self.links];
//            NSLog(@"parsed");
//        }];
    }
    if ([parsedData isKindOfClass:[NSArray class]]) {
        //modify if array
        NSArray* myData = (NSArray*) parsedData;
    }
}

-(void) postJson {
    NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://78.90.84.177/index.json"]];
    [req setHTTPMethod:@"PUT"];
    NSString* string = @"{{\"name\":\"Mitko\"}}";
//    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@[@1]];
    id data1 = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];

//    id data = [NSJSONSerialization JSONObjectWithData:@[@"t", @"t"] options:NS error:<#(NSError *__autoreleasing *)#>];
    
    [req setHTTPBody:data1];
//    [req setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];

    [NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:nil];
}
@end
